import { editUserService, UserData } from "@/services/api/userList";
import { useRequest } from "ahooks";
import { Button, Form, Input, message, Radio, Space } from "antd";
import React, { useEffect } from "react";

interface EditFormProps {
  data?: Partial<UserData>;
  onSubmit?: () => void;
}

const EditForm: React.FC<EditFormProps> = ({ data, onSubmit = () => {} }) => {
  console.log("EditForm更新");
  const [form] = Form.useForm<UserData>();

  const { run } = useRequest(editUserService, {
    manual: true,
    onSuccess(data, params) {
      message.info(data);
      onSubmit();
      form.resetFields();
    },
  });

  useEffect(() => {
    if (!data) {
        return;
    }
    if (Object.keys(data).length !== 0) {
      form.setFieldsValue(data);
    } else {
      form.resetFields();
    }
  }, [data]);

  async function submit() {
    try {
      await form.validateFields();
      const data = form.getFieldsValue(true);
      run(data);
    } catch (error) {
      console.log(error);
    }
  }

  return (
    <>
      <Form form={form}>
        <Form.Item
          name="name"
          label="客户名称"
          rules={[{ required: true, message: "客户名称不能为空！" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="tel"
          label="联系电话"
          rules={[{ required: true, message: "联系电话不能为空！" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="business"
          label="所属行业"
          rules={[{ required: true, message: "所属行业不能为空！" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="statue"
          label="经营状态"
          rules={[{ required: true, message: "经营状态不能为空！" }]}
        >
          <Radio.Group>
            <Radio value="1">营业中</Radio>
            <Radio value="2">暂停营业</Radio>
            <Radio value="3">已关闭</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item
          name="email"
          label="邮箱"
          rules={[{ required: true, message: "邮箱不能为空！" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="creditCode"
          label="统一信用代码"
          rules={[{ required: true, message: "统一信用代码不能为空！" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="industryNum"
          label="工商注册号"
          rules={[{ required: true, message: "工商注册号不能为空！" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="organizationCode"
          label="组织机构代码"
          rules={[{ required: true, message: "组织机构代码不能为空！" }]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          name="legalPerson"
          label="法人名"
          rules={[{ required: true, message: "法人名不能为空！" }]}
        >
          <Input />
        </Form.Item>
      </Form>
      <div style={{ textAlign: "right" }}>
        <Button type="primary" onClick={submit}>
          提交
        </Button>
      </div>
    </>
  );
};

export default EditForm;
